//
//  CZComment.swift
//  GZWeibo007
//
//  Created by 蓝文举 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class CZComment: NSObject {
    
    //MARK: - 属性
    
    ///评论创建时间
    var created_at: String?
    
    ///评论的ID
    var id: Int = 0
    
    ///评论的内容
    var text: String?
    
    ///评论的来源
    var source: String?
    
    ///评论作者的用户信息字段
    var user: CZUser?
    
    ///评论的MID
    var mid: String?
    
    ///评论的微博信息字段
    var status: CZStatus?
    
    ///评论来源评论，当本评论属于对另一评论的回复时返回此字段
    var reply_comment: CZComment?
    
    ///行高
    var rowHeight: CGFloat?
    
    //MARK: - 构造方法
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                user = CZUser(dict: dict)
            }
           return
        }
        super.setValue(value, forKey: key)
    }
    
    //MARK: - 读取评论数据
    class func loadComments(id: Int,max_id: Int, finished: (comments: [CZComment]?, error: NSError?) -> ()) {
        CZNetworkTool.sharedInstance.loadComments(id, max_id: max_id) { (result, error) -> () in
            if error != nil || result == nil {
                return
            }
            if let array = result!["comments"] as? [[String: AnyObject]] {
                var comments = [CZComment]()
            
            for dict in array {
                let comment = CZComment(dict: dict)
                comments.append(comment)
            }
            finished(comments: comments, error: nil)
            }
        }
    }
}
